package mn.slide.petrovis.news;

import java.util.ArrayList;

import mn.slide.petrovis.R;
import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

public class NewsTabGroupActivity extends ActivityGroup
{

	public static NewsTabGroupActivity	group;
	private ArrayList<View>				history;
	Animation							animationSlideInLeft;
	Animation							outToLeft , inFromRight , inFromLeft ,
			outToRight;

	public NewsTabGroupActivity()
	{
	}

	@Override
	protected void onCreate( Bundle savedInstanceState )
	{
		super.onCreate(savedInstanceState);
		this.history = new ArrayList<View>();
		group = this;
		View view = getLocalActivityManager().startActivity("NewsAct", new Intent(this, NewsActivity.class)).getDecorView();
		replaceView(view);
		animationSlideInLeft = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);
		animationSlideInLeft.setDuration(500);
	}

	public void replaceView( View v )
	{
		if (history.size() != 0)
		{
			outToLeft = AnimationUtils.loadAnimation(this, R.anim.out_to_left);
			View v1 = history.get(history.size() - 1);
			v1.setAnimation(outToLeft);
			inFromRight = AnimationUtils.loadAnimation(this, R.anim.in_from_right);
			v.setAnimation(inFromRight);
		}
		history.add(v);
		setContentView(v);
	}

	public void back()
	{
		inFromLeft = AnimationUtils.loadAnimation(this, R.anim.in_from_left);
		outToRight = AnimationUtils.loadAnimation(this, R.anim.out_to_right);
		if (history.size() > 1)
		{
			View v = history.get(history.size() - 1);
			v.setAnimation(outToRight);
			history.remove(history.size() - 1);
			v = history.get(history.size() - 1);
			v.setAnimation(inFromLeft);
			setContentView(v);

		}
		else
		{
			finish();
		}
	}

	@Override
	public void onBackPressed()
	{
		if (history.size() != 1)
			NewsTabGroupActivity.group.back();
		else
			System.exit(0);
		return;
	}

}